package com.cimp.controller.fuse;

import com.cimp.domain.common.Result;
import com.cimp.domain.fuse.*;
import com.cimp.util.fuse.Contants;
import com.cimp.util.fuse.HttpUtil;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("fuse")
public class FuseController {

    private List<CallBackVo> cbList;

    @RequestMapping("signature")
    public String generateSignature(Model model, BaseInfo baseInfo){
        if (StringUtils.isNotBlank(baseInfo.getUserName())) {
            long l = System.currentTimeMillis();
            String md5Hex = DigestUtils.md5Hex(baseInfo.getUserName() + l + Contants.KEY);
            baseInfo.setTimeStamp(l);
            baseInfo.setSignature(md5Hex);
        }
        return "fuse/signature";
    }

    @RequestMapping("inject/single")
    public String postSingle(PostBean postBean, Model model){
        if(StringUtils.isNotBlank(postBean.getUrl())){
            Result result = HttpUtil.postMessage(postBean.getUrl(), postBean.getContent());
            JSONObject object = JSONObject.fromObject(result);
            model.addAttribute("result", object.toString());
        }else{
            postBean.setUrl(Contants.DEFAULT_SINGLE_URL);
        }
        return "fuse/inject/postSingle";
    }

    @RequestMapping("inject/series")
    public String postSeries(PostBean postBean, Model model){
        if(StringUtils.isNotBlank(postBean.getUrl())){
            Result result = HttpUtil.postMessage(postBean.getUrl(), postBean.getContent());
            JSONObject object = JSONObject.fromObject(result);
            model.addAttribute("result", object.toString());
        }else{
            postBean.setUrl(Contants.DEFAULT_SERIES_URL);
        }
        return "fuse/inject/postSeries";
    }

    @RequestMapping("inject/callback/post")
    @ResponseBody
    public Result injectCallBack(@RequestBody String body){
        Result result = null;
        try{
            JSONObject jsonObject = JSONObject.fromObject(body);
            CallBack callBack = (CallBack) JSONObject.toBean(jsonObject, CallBack.class);
            if(callBack != null){
                //签名校验
                String md5Hex = DigestUtils.md5Hex(callBack.getUserName() + callBack.getTimeStamp() + Contants.KEY);
                if(!md5Hex.equals(callBack.getSignature())){
                    return Result.error("Signature check failure.");
                }
            }else{
                return Result.error("json parse error");
            }
            if (cbList == null) {
                cbList = new ArrayList<>();
            }
            String format = new SimpleDateFormat("HH:mm:ss").format(new Date());
            CallBackVo callBackVo = new CallBackVo(format, body);
            cbList.add(callBackVo);
            result = Result.success();

        }catch (JSONException e){
            Result.error("callback body can not convert to bean");
        }
        return result;
    }

    @RequestMapping("inject/callback/page")
    public String toCallBackPage(){
        return "fuse/inject/callBack";
    }

    @RequestMapping("inject/callback")
    @ResponseBody
    public List<CallBackVo> getCallBackMessage(){
        return this.cbList;
    }
}
